<template>
  <el-container class="home-container">
    <!-- 页面头部区域 -->
    <el-header>
      <div>
        <img src="../assets/ECM.png" alt="ECM" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏导航区域 -->
      <el-aside :width="isCollapsed ? '64px' : '200px'">
        <div class="toggle-menu-btn" @click="toggleMenuCollapse">{{strToggle}}</div>
        <el-menu
          :default-active="$route.path"
          class=""
          background-color="#568066"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapsed"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="`''${item.id}`" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-setting"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="`/${itemSub.path}`" v-for="itemSub in item.children" :key="itemSub.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{itemSub.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  created() {
    this.getMenuList()
  },
  data() {
    return {
      menuList: [],
      isCollapsed: false,
      strToggle: '><'
    }
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏导航项目列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.menuList = res.data
    },
    // 切换侧边栏样式
    toggleMenuCollapse () {
      this.isCollapsed = !this.isCollapsed
      this.strToggle = this.isCollapsed ? '<>' : '><'
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #557055;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    img {
      width: 60px;
      height: 40px;
    }

    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #568066;

  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #559066;
}
.home-container {
  height: 100%;
}
.toggle-menu-btn {
  background-color: #456123;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  color: #ffffff;
}
</style>
